package com.job.landlord;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.job.common.entity.Role;
import com.job.common.entity.SystemUser;
import com.job.landlord.mapper.UserRoleMapper;
import com.job.landlord.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ClientUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SystemUser user = null;
//        Object obj = redisTemplate.opsForValue().get("systemUser:landlord:" + username);
//        if (obj != null) {
//            user = JSONUtil.toBean(obj.toString(), SystemUser.class);
//        }
//        String roleList = redisTemplate.opsForValue().get("systemUser:role:landlord:" + username);
//        List<Role> roles = JSONUtil.toList(JSONUtil.parseArray(roleList), Role.class);
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        for (Role role : roles) {
//            authorities.add(new SimpleGrantedAuthority(role.getDescription()));
//        }
//        if (user != null) {
//            return new ClientUserDetails(user, authorities);
//        }
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, username);
        List<SystemUser> userList = systemUserService.list(queryWrapper);

        if (CollectionUtil.isEmpty(userList)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        user = userList.get(0);
//        redisTemplate.opsForValue().set("systemUser:landlord:" + username, JSONUtil.toJsonStr(user), 60 * 60 * 5, TimeUnit.SECONDS);
        List<Role> roless = userRoleMapper.getRolesByUserId(user.getId());
        List<GrantedAuthority> authorities1 = new ArrayList<>();
        for (Role role : roless) {
            authorities1.add(new SimpleGrantedAuthority(role.getDescription()));
        }
//        redisTemplate.opsForValue().set("systemUser:role:landlord:" + username, JSONUtil.toJsonStr(roless), 60 * 60 * 5, TimeUnit.SECONDS);
        return new ClientUserDetails(user, authorities1);

    }
}
